/*
' Copyright (c) 2014 Diadrasis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using net.diadrasis.dnn.jsImageGallery.Components;
using DotNetNuke.Common;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.FileSystem;

namespace net.diadrasis.dnn.jsImageGallery
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditjsImageGallery class is used to manage content
    /// 
    /// Typically your edit control would be used to create new content, or edit existing content within your module.
    /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
    /// 
    /// Because the control inherits from jsImageGalleryModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class EditImageSections : jsImageGalleryModuleBase
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try{

                if (Page.IsPostBack == false)
                {
                    gvImageSections.DataSource = cImageSectionController.GetImageSections(ModuleId);
                    gvImageSections.DataBind();

                    //set column width
                    foreach (DataControlField column in gvImageSections.Columns){
                        if (column.HeaderText == "Description")
                        {
                            column.ItemStyle.Width = Unit.Pixel(200);
                        }
                        else
                        {
                            column.ItemStyle.Width = Unit.Pixel(75);
                        }
                    }

                    this.addBtn.Attributes.Add("onclick", "return " + UrlUtils.PopUpUrl(Globals.NavigateURL("AddSection", "mid=" + this.ModuleId.ToString()), this, PortalSettings, true, false));
                    for (int i = 0; i < gvImageSections.Rows.Count; i++)
                    {
                        gvImageSections.Rows[i].Cells[getColumnID("Edit Images")].Attributes.Add("onclick", "return " + UrlUtils.PopUpUrl(Globals.NavigateURL("EditImages", "mid=" + this.ModuleId.ToString() + "&imgsecid=" + gvImageSections.DataKeys[i]["id"]), this, PortalSettings, true, false));
                        gvImageSections.Rows[i].Cells[getColumnID("Edit Section")].Attributes.Add("onclick", "return " + UrlUtils.PopUpUrl(Globals.NavigateURL("AddSection", "mid=" + this.ModuleId.ToString() + "&imgsecid=" + gvImageSections.DataKeys[i]["id"]), this, PortalSettings, true, false));
                    }
                }
                
            }
            catch (Exception exc){ //Module failed to load            
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion
            

        //to avoid refering to cell index
        protected int getColumnID(string columnName){
            int columnID=0;           
            foreach (DataControlField column in gvImageSections.Columns){
                if (column.HeaderText == columnName) {
                    columnID = gvImageSections.Columns.IndexOf(column);
                    break;
                }             
            }
            return columnID;
        }

        protected void gvImageSections_RowCommand(Object sender, GridViewCommandEventArgs e){
            
                if (e.CommandName == "Delete"){
                    string message = e.CommandArgument.ToString();
                    cImageSectionController.DeleteImageSection(Convert.ToInt32(message));
                       Response.Redirect(Request.RawUrl, true);  
                }
                else
                {
                    Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
                }
        }
    }
}